package com.evan.wj.controller;


import com.evan.wj.pojo.Menu;
import com.evan.wj.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class MenuContraller {
    @Autowired
    MenuService menuService;

    @GetMapping("/api/menu")
    public List<Menu> list() throws Exception {
        return menuService.list();
    }

    @GetMapping("/api/menu/{cid}/{user}")
    public List<Menu> listByCategory(@PathVariable("cid") Integer cid,@PathVariable("user")String user) throws Exception {
        System.out.println(user+"+++");
        if (!user.equals("admin")){
            List<Menu> allByParentIdIsAndTypeIs = menuService.findAllByParentIdIsAndTypeIs(cid, "0");
            System.out.println(allByParentIdIsAndTypeIs.size());
            for (int i=0;i<allByParentIdIsAndTypeIs.size();i++) {
                Menu menu = allByParentIdIsAndTypeIs.get(i);

                List<Menu> allByParentIdIs1 = menuService.findAllByParentIdIs(menu.getId() );
                if(0==allByParentIdIs1.size()){
                    menu.setLeaf(true);
                    allByParentIdIsAndTypeIs.set(i,menu);
                }
            }
            return allByParentIdIsAndTypeIs;
        }
        System.out.println("--------------");

        List<Menu> allByParentIdIs = menuService.findAllByParentIdIs(cid);
        for (int i=0;i<allByParentIdIs.size();i++) {
            Menu menu = allByParentIdIs.get(i);
            List<Menu> allByParentIdIs1 = menuService.findAllByParentIdIs(menu.getId() );
            if(0==allByParentIdIs1.size()){
                menu.setLeaf(true);
                allByParentIdIs.set(i,menu);
            }
        }
        return allByParentIdIs;
    }
}
